<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会议室名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入会议室名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地理位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入地理位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼层" prop="floor">
        <el-input
          v-model="queryParams.floor"
          placeholder="请输入楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="容纳人数" prop="capacity">
        <el-input
          v-model="queryParams.capacity"
          placeholder="请输入容纳人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否VIP" prop="isVip">
        <el-select v-model="queryParams.isVip" placeholder="请选择是否VIP" clearable>
          <el-option label="VIP" value="1" />
          <el-option label="普通" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['meetingroom:meetingroom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['meetingroom:meetingroom:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['meetingroom:meetingroom:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="meetingRoomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会议室ID" align="center" prop="roomId" />
      <el-table-column label="会议室名称" align="center" prop="roomName" />
      <el-table-column label="地理位置" align="center" prop="location" />
      <el-table-column label="楼层" align="center" prop="floor" />
      <el-table-column label="容纳人数" align="center" prop="capacity" />
      <el-table-column label="是否VIP" align="center" prop="isVip">
        <template slot-scope="scope">
          <span>{{ scope.row.isVip === '1' ? 'VIP' : '普通' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备" align="center" prop="equipment" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
            <el-tag :type="scope.row.status === '0' ? 'success' : 'danger'">
                {{ scope.row.status === '0' ? '正常' : '停用' }}
            </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['meetingroom:meetingroom:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['meetingroom:meetingroom:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="会议室名称" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入会议室名称" />
        </el-form-item>
        <el-form-item label="地理位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入地理位置" />
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入楼层" />
        </el-form-item>
        <el-form-item label="容纳人数" prop="capacity">
          <el-input-number v-model="form.capacity" :min="1" placeholder="请输入容纳人数" style="width: 100%" />
        </el-form-item>
        <el-form-item label="是否VIP" prop="isVip">
          <el-radio-group v-model="form.isVip">
            <el-radio label="1">VIP</el-radio>
            <el-radio label="0">普通</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="设备" prop="equipment">
          <el-input v-model="form.equipment" placeholder="请输入设备，多个用逗号分隔" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listMeetingRoom, addMeetingRoom, getMeetingRoom, updateMeetingRoom, deleteMeetingRoom } from "@/api/meetingroom";

export default {
  name: "MeetingRoom",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会议室表格数据
      meetingRoomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomName: undefined,
        location: undefined,
        floor: undefined,
        capacity: undefined,
        isVip: undefined
      },
      // 新增/编辑表单数据
      form: {
        roomName: '',
        location: '',
        floor: '',
        capacity: 1,
        isVip: '0',
        equipment: '',
        status: '0',
        remark: ''
      },
      // 表单校验规则
      rules: {
        roomName: [{ required: true, message: '会议室名称不能为空', trigger: 'blur' }],
        location: [{ required: true, message: '地理位置不能为空', trigger: 'blur' }],
        floor: [{ required: true, message: '楼层不能为空', trigger: 'blur' }],
        capacity: [{ required: true, type: 'number', message: '容纳人数不能为空', trigger: 'blur' }],
        isVip: [{ required: true, message: '请选择是否VIP', trigger: 'change' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会议室列表 */
    getList() {
      this.loading = true;
      listMeetingRoom(this.queryParams).then(response => {
        this.meetingRoomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roomId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm('form');
      this.title = '新增会议室';
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const roomId = row && row.roomId ? row.roomId : (this.ids.length === 1 ? this.ids[0] : null);
      if (!roomId) {
        this.$modal.msgWarning('请选择要修改的会议室');
        return;
      }
      getMeetingRoom(roomId).then(res => {
        if (res.code === 200) {
          this.resetForm('form');
          this.form = Object.assign({}, this.form, res.data);
          this.title = '编辑会议室';
          this.open = true;
        } else {
          this.$modal.msgError(res.msg || '获取详情失败');
        }
      });
    },
    /** 提交新增/编辑表单 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const isEdit = !!this.form.roomId;
          const submitApi = isEdit ? updateMeetingRoom : addMeetingRoom;
          // 编辑时不提交 roomId 以外的只读字段
          const submitData = isEdit ? {
            roomId: this.form.roomId,
            roomName: this.form.roomName,
            location: this.form.location,
            floor: this.form.floor,
            capacity: this.form.capacity,
            isVip: this.form.isVip,
            equipment: this.form.equipment,
            status: this.form.status,
            remark: this.form.remark
          } : this.form;
          submitApi(submitData).then(res => {
            if (res.code === 200) {
              this.$modal.msgSuccess(isEdit ? "修改成功" : "新增成功");
              this.open = false;
              this.getList();
            } else {
              this.$modal.msgError(res.msg || (isEdit ? "修改失败" : "新增失败"));
            }
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roomIds = row.roomId || this.ids;
      this.$modal.confirm('是否确认删除会议室编号为"' + roomIds + '"的数据项？').then(() => {
        return deleteMeetingRoom(roomIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script> 